package com.kx.websocket.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @ClassName MessageController
 * @Author Sun
 * @Date 2021/6/1 20:03
 */
@RestController
@RequestMapping("/messgae")
public class MessageController {

    private ObjectMapper objectMapper = new ObjectMapper();

    @Autowired
    private ApplicationContext context; //用于发布事件
    public String sendMessage(String message) throws JsonProcessingException {
        //把前端发送过的json数据数据存入到map中
        //约定map中key和value
        //to 发送给谁
        //from 谁发送的
        //content 内容是什么
        Map map = objectMapper.readValue(message, Map.class);

        context.publishEvent(map);

        return "success";
    }
}
